package cn.wenhe9.novel.controller.front;

import cn.wenhe9.novel.auth.UserHolder;
import cn.wenhe9.novel.constant.ApiRouterConsts;
import cn.wenhe9.novel.constant.ResultCodeEnum;
import cn.wenhe9.novel.constant.SystemConfigConsts;
import cn.wenhe9.novel.domain.dto.UserFeedbackDto;
import cn.wenhe9.novel.domain.dto.UserInfoDto;
import cn.wenhe9.novel.domain.dto.UserLoginDto;
import cn.wenhe9.novel.domain.dto.UserRegisterDto;
import cn.wenhe9.novel.domain.entity.user.UserFeedback;
import cn.wenhe9.novel.domain.vo.*;
import cn.wenhe9.novel.result.PageResultDto;
import cn.wenhe9.novel.result.ResultResponse;
import cn.wenhe9.novel.service.BookService;
import cn.wenhe9.novel.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 会员模块相关接口 API
 *
 * @author DuJinliang
 * 2022/5/18
 */
@Api(tags = "会员模块相关接口 API")
@RestController
@RequestMapping(ApiRouterConsts.API_FRONT_USER_URL_PREFIX)
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private BookService bookService;

    /**
     * 用户注册接口
     */
    @ApiOperation("用户注册接口")
    @PostMapping("register")
    public ResultResponse<UserRegisterDto> register(@Valid @RequestBody UserRegisterVo vo) {
        return userService.register(vo);
    }

    /**
     * 用户登录接口
     */
    @ApiOperation("用户登录接口")
    @PostMapping("login")
    public ResultResponse<UserLoginDto> login(@Valid @RequestBody UserLoginVo vo) {
        return userService.login(vo);
    }


    /**
     * 用户信息修改接口
     */
    @PutMapping
    @ApiOperation("用户信息修改接口")
    public ResultResponse<Void> updateUserInfo(@Valid @RequestBody UserInfoUpdateVo vo) {
        return userService.updateUserInfo(UserHolder.getUserId(), vo);
    }

    /**
     * 分页查询我的反馈列表
     * */
    @ApiOperation("分页查询我的反馈列表")
    @GetMapping("feedback/list/{page}/{limit}")
    public ResultResponse<PageResultDto<UserFeedbackDto>> listUserFeedBackByPage(
            @PathVariable("page") @DefaultValue("1") Long page,
            @PathVariable("limit") @DefaultValue("5") Long limit
    ){
        return ResultResponse.ok(userService.listFeedBack(UserHolder.getUserId(), page, limit));
    }

    /**
     * 用户反馈提交接口
     */
    @ApiOperation("用户反馈提交接口")
    @PostMapping("feedback")
    public ResultResponse<Void> submitFeedback(@RequestBody String content) {
        return userService.saveFeedback(UserHolder.getUserId(), content);
    }

    /**
     * 用户反馈删除接口
     * */
    @ApiOperation("用户反馈删除接口")
    @DeleteMapping("feedback/{id}")
    public ResultResponse<Void> deleteFeedback(@PathVariable Long id) {
        return userService.deleteFeedback(UserHolder.getUserId(), id);
    }

    /**
     * 发表评论接口
     * */
    @ApiOperation("发表评论接口")
    @PostMapping("comment")
    public ResultResponse<Void> comment(@RequestBody UserCommentVo vo) {
        vo.setUserId(UserHolder.getUserId());
        userService.saveComment(vo);
        bookService.saveComment(vo);
        return ResultResponse.ok();
    }

    /**
     * 修改评论接口
     */
    @ApiOperation("修改评论接口")
    @PutMapping("comment/{id}")
    public ResultResponse<Void> updateComment(@PathVariable Long id, String content) {
        return bookService.updateComment(UserHolder.getUserId(), id, content);
    }

    /**
     * 删除评论接口
     */
    @ApiOperation("删除评论接口")
    @DeleteMapping("comment/{id}")
    public ResultResponse<Void> deleteComment(@PathVariable Long id) {
        return bookService.deleteComment(UserHolder.getUserId(), id);
    }

    /**
     * 查询书架状态接口
     * 0-不在书架
     * 1-已在书架
     */
    @ApiOperation("查询书架状态接口")
    @PostMapping("bookshelf_status")
    public ResultResponse<Integer> getBookshelfStatus(@RequestBody String bookId) {
        return userService.getBookshelfStatus(UserHolder.getUserId(), bookId);
    }

    /**
     * 加入书架
     * */
    @ApiOperation("加入书架")
    @PostMapping("bookshelf")
    public ResultResponse<Void> addToBookShelf(@RequestBody UserBookshelfVo vo) {
        userService.addToBookShelf(UserHolder.getUserId(), vo.getBookId(), vo.getPreContentId());
        return ResultResponse.ok();
    }

    /**
     * 移出书架
     * */
    @ApiOperation("移出书架")
    @DeleteMapping("bookshelf/{bookId}")
    public ResultResponse<?> removeFromBookShelf(@PathVariable("bookId") Long bookId) {
        userService.removeFromBookShelf(UserHolder.getUserId(), bookId);
        return ResultResponse.ok();
    }

    /**
     * 添加阅读记录
     * */
    @ApiOperation("添加阅读记录")
    @PostMapping("readHistory")
    public ResultResponse<?> addReadHistory(@RequestBody UserBookshelfVo vo) {
        userService.addReadHistory(UserHolder.getUserId(), vo.getBookId(), vo.getPreContentId());
        return ResultResponse.ok();
    }

    /**
     * 分页查询阅读记录
     * */
    @ApiOperation("分页查询阅读记录")
    @GetMapping("readHistory/list/{page}/{limit}")
    public ResultResponse<?> listReadHistoryByPage(
            @PathVariable("page") @DefaultValue("1") Long page,
            @PathVariable("limit") @DefaultValue("5") Long limit
    ) {
        return ResultResponse.ok(userService.listReadHistoryByPage(UserHolder.getUserId(), page, limit));
    }

    /**
     * 分页查询用户书评
     * */
    @ApiOperation("分页查询用户书评")
    @GetMapping("comment/list/{page}/{limit}")
    public ResultResponse<?> listCommentByPage(
            @PathVariable("page") @DefaultValue("1") Long page,
            @PathVariable("limit") @DefaultValue("5") Long limit
    ) {
        return ResultResponse.ok(userService.listCommentByPage(UserHolder.getUserId(), page, limit));
    }


    /**
     * 查询个人信息
     */
    @ApiOperation("查询个人信息")
    @GetMapping("userInfo")
    public ResultResponse<UserInfoDto> userInfo() {
        return userService.getUserInfo(UserHolder.getUserId());
    }

    /**
     * 更新密码
     * */
    @ApiOperation("更新密码")
    @PostMapping("updatePassword")
    public ResultResponse<Void> updatePassword(@RequestBody UserPasswordUpdateVo vo) {
        Long userId = UserHolder.getUserId();
        userService.updatePassword(userId, vo);
        return ResultResponse.ok();
    }

    /**
     * 分页查询书架
    */
    @ApiOperation("分页查询书架")
    @GetMapping("bookshelf/list/{page}/{limit}")
    public ResultResponse<?> listBookShelfByPage(
            @PathVariable("page") @DefaultValue("1") Long page,
            @PathVariable("limit") @DefaultValue("5") Long limit
    ) {
        return ResultResponse.ok(userService.listUserBookShelfByPage(UserHolder.getUserId(), page, limit));
    }
}
